local skels = {}

local zongkui = fk.CreateSkill{
    name = "XD__zongkui",
}
Fk:loadTranslationTable{
    ["XD__zongkui"] = "纵傀",
    [":XD__zongkui"] = "游戏开始时，除外你的其他备选武将牌；每轮开始时，你可以随机获得至多两张武将牌并除外；以此法除外的武将牌称为“傀”（数量至多为7）；合理的时机，你可以发动一张“傀”上的技能（相同的时点只能发动一个技能），然后弃置此“傀”。"..
    "<br><a href = 'XD_zongkui_href'><font color='#b97eb4'>何意味……</font></a>",
    ["XD_zongkui_href"] = "关于备选武将牌<br>由于部分模式不提供备选武将牌，改为除外随机X张武将牌。<br>（X为房间设置选将数目-1，且至多为7）",
    ["@&XD__zongkui"] = "傀",
    ["XD__zongkui_active"] = "纵傀",
    ["#XD__zongkui"] = "纵傀：是否随机获得至多%arg张武将牌并除外",
}
local U = require "packages/utility/utility"
local function setKui(player, is_start, is_death)
    local room = player.room
    local skills = {}
    for _, g in ipairs(player:getTableMark("@&XD__zongkui")) do
        local general = Fk.generals[g]
        for _, s in ipairs(general:getSkillNameList()) do
            local skill = Fk.skills[s]
            if not (player:hasSkill(s, true) and not player:isFakeSkill(Fk.skills[s])) and skill:getSkeleton() and
            not (skill:hasTag(Skill.Lord) and player.role ~= "lord") and not (skill:hasTag(Skill.AttachedKingdom) and not table.contains(skill:getSkeleton().attached_kingdom, player.kingdom)) and
            not skill:isInstanceOf(StatusSkill) then
                table.insertIfNeed(skills, s)
            end
        end
    end
    local fangke_skills = player:getTableMark("XD__zongkui_skills")
    if #fangke_skills > 0 then
        for _, skill in ipairs(fangke_skills) do
            if not table.contains(skills, skill) then
                player:loseFakeSkill(skill)
                Fk.skill_skels[skill]:onLose(player, is_death)
            end
        end
    end
    if #skills > 0 then
        for _, skill in ipairs(skills) do
            if not table.contains(fangke_skills, skill) then
                player:addFakeSkill(skill)
                player:prelightSkill(skill, true)
                Fk.skill_skels[skill]:onAcquire(player, is_start)
            end
        end
    end
    room:setPlayerMark(player, "XD__zongkui_skills", skills)
end
zongkui:addEffect(fk.GameStart, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(zongkui.name) and #player.room.general_pile > 0
    end,
    on_cost = Util.TrueFunc,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local generals = table.random(room.general_pile, math.min(7, room.settings.generalNum - 1))
        if room:isGameMode("m_1v1_mode") then
            generals = room:getBanner(player.role == "lord" and "@&firstGenerals" or "@&secondGenerals")
            room:setBanner(player.role == "lord" and "@&firstGenerals" or "@&secondGenerals", {})
        end
        for _, general in ipairs(generals) do
            room:findGeneral(general)
        end
        local mark = player:getTableMark("@&XD__zongkui")
        table.insertTable(mark, generals)
        room:setPlayerMark(player, "@&XD__zongkui", mark)
        if player:hasSkill("XD__zongkui", true) then
            setKui(player, true)
        end
    end,
})
zongkui:addEffect(fk.TurnStart, {
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(zongkui.name) and #player:getTableMark("@&XD__zongkui") < 7 and
        #player.room.general_pile > 0
    end,
    on_cost = function (self, event, target, player, data)
        local max = 1
        if #player:getTableMark("@&XD__zongkui") < 6 then max = 2 end
        local bool, dat = player.room:askToUseActiveSkill(player, {
            skill_name = "XD__zongkui_active",
            skip = true,
            prompt = "#XD__zongkui:::"..U.ConvertNumber(max)
        })
        if bool and dat then
            event:setCostData(self, dat.interaction)
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local generals = table.random(room.general_pile, event:getCostData(self))
        if type(generals) ~= "table" then generals = {generals} end
        for _, general in ipairs(generals) do
            room:findGeneral(general)
        end
        local mark = player:getTableMark("@&XD__zongkui")
        table.insertTable(mark, generals)
        room:setPlayerMark(player, "@&XD__zongkui", mark)
        if player:hasSkill("XD__zongkui", true) then
            setKui(player)
        end
    end,
})
zongkui:addEffect(fk.AfterSkillEffect, {
    anim_type = "drawcard",
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(zongkui.name) and #player:getTableMark("@&XD__zongkui") > 0 and
        not data.skill.is_delay_effect and table.contains(player:getTableMark("XD__zongkui_skills"), data.skill:getSkeleton().name)
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local choices = player:getMark("@&XD__zongkui")
        local owner = table.find(choices, function (name)
            local general = Fk.generals[name]
            return table.contains(general:getSkillNameList(), data.skill:getSkeleton().name)
        end) or "?"
        if owner ~= "?" then
            room:removeTableMark(player, "@&XD__zongkui", owner)
            room:returnToGeneralPile({owner})
            setKui(player)
        end
    end,
})
zongkui:addEffect(fk.SkillEffect, {
    can_refresh = function (self, event, target, player, data)
        return target == player and table.contains(player:getTableMark("XD__zongkui_skills"), data.skill:getSkeleton().name)
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        --event.invoked_times = event.invoked_times or {}
        --[[local logic = room.logic
        local skill = Fk.skills[data.skill:getSkeleton().name]
        if skill:isInstanceOf(TriggerSkill) then
            ---@cast skill TriggerSkill
            local skillEvent = skill.event
            if type(skillEvent) == "table" and skillEvent:isSubclassOf(TriggerEvent) and skillEvent.invoked_times then
                p(skillEvent.invoked_times)
            end
        end]]--
        --[[for key, value in pairs(room.logic.skill_table) do
            if type(key) == "table" and key:isSubclassOf(TriggerEvent) and key.invoked_times then
                for _, skillName in ipairs(player:getTableMark("XD__zongkui_skills")) do
                    key.invoked_times[skillName] = -1
                end
            end
        end]]--
        for _, s in ipairs(Fk.skills[data.skill:getSkeleton().name].related_skills) do
            if s:isInstanceOf(StatusSkill) then
                room:addSkill(s)
            end
        end
    end,
})
zongkui:addLoseEffect(function (self, player, is_death)
    local room = player.room
    if player:getMark("@&XD__zongkui") ~= 0 then
        for _, general in ipairs(player:getMark("@&XD__zongkui")) do
            room:returnToGeneralPile({general})
        end
        room:setPlayerMark(player, "@&XD__zongkui", 0)
    end
    setKui(player, false, is_death)
end)
zongkui:addEffect("invalidity", {
    recheck_invalidity = true,
    invalidity_func = function (self, from, skill)
        return not from:hasSkill(zongkui.name) and from:getMark("XD__zongkui_skills") ~= 0 and
        table.contains(from:getTableMark("XD__zongkui_skills"), skill.name)
    end,
})
table.insert(skels, zongkui)

local zongkui_active = fk.CreateSkill{
    name = "XD__zongkui_active",
}
zongkui_active:addEffect("active", {
    interaction = function (self, player)
        local max = 1
        if #player:getTableMark("@&XD__zongkui") < 6 then max = 2 end
        return UI.Spin{
            from = 1,
            to = max,
            default = max,
        }
    end,
    card_num = 0,
    card_filter = Util.FalseFunc,
    target_num = 0,
    target_filter = Util.FalseFunc,
})
table.insert(skels, zongkui_active)

return skels